package com.lightsh.sensor;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;

public class LightSensor extends BaseSensor{
	
	private enum LightState{
		INIT,
		ON,
		OFF
	}
	
	private LightState mCurrentState = LightState.INIT;

	public LightSensor(Context context) {
		super(context);
	}

	@Override
	protected int getSensorType() {
		return Sensor.TYPE_LIGHT;
	}

	@Override
	protected String getLogTag() {
		return "LightSensorController";
	}

	@Override
	protected void onSensorChanged(SensorEvent event) {
		final float value = event.values[0];
		LightState state = fetchCurrentState(value);
		if(state != mCurrentState){
			final boolean isInit = mCurrentState == LightState.INIT;
			mCurrentState = state;
			if(!isInit){
				onLightStateChanged(value);
			}else{
				logW("init state = " + mCurrentState + ", value = " + value);
			}
		}
	}

	private LightState fetchCurrentState(float value){
		if(value >= 10f){
			return LightState.ON;
		}else{
			return LightState.OFF;
		}
	}
	
	private void onLightStateChanged(float value){
		logD("light state change to " + mCurrentState + ", value = " + value);
		VibratorHelper.vibrate(mContext, 500);
	}
}
